package com.cxm.homework5.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements InvocationHandler {

    private UserDao userDao;

    public Object createProxy(UserDao userDao){
        this.userDao=userDao;
        ClassLoader classLoader = JdkProxy.class.getClassLoader();

        Class[] interfaces = userDao.getClass().getInterfaces();

        return Proxy.newProxyInstance(classLoader,interfaces,this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        UserDaoAspect userDaoAspect = new UserDaoAspect();

        userDaoAspect.check();
        Object obj = method.invoke(userDao, args);
        userDaoAspect.log();

        return obj;
    }
}
